<script lang="ts" setup>
import TitleContent from '@/components/TitleContent.vue'
import TagComtent from '@/components/TagComtent.vue'
import imgValue from '@/assets/svg/password.svg'
import type { PwdForm } from '@/model'
import { ref } from 'vue'
import HeadContent from '@/components/HeadContent.vue'
import { UseUserStore } from '@/stores'
import { upadtePwd } from '@/api/user'

// 修改密码
const onSubmit = async () => {
  const res = await upadtePwd(pwdForm.value)
  console.log(res.data);
  ElMessage.success(res.data)
  onReset()
  
  // 重置密码
}
const pwdForm = ref<PwdForm>({
  oldPassword: '',
  newPassword: '',
  surePassowrd: '',
  email: UseUserStore().user.email
})

// 重置
const onReset = () => {
  pwdForm.value = {
    email: UseUserStore().user.email,
    oldPassord: '',
    newPassword: '',
    surePassowrd: ''
  }
}

// 规则123456789
const checkOldSame = (rule: any, value: any, cb: (arg0: Error) => void) => {
  if (value === pwdForm.value.oldPassword) {
    cb(new Error('原密码和新密码相同'))
  }
}

const checkNewSame = (rule: any, value: any, cb: (arg0: Error) => void) => {
  if (value !== pwdForm.value.newPassword) {
    cb(new Error('新密码和确认密码不一样'))
  } 
  
}
const rules = {
  // 原密码
  oldPassword: [
    { required: true, message: '原密码不能为空', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是6-15位的非空字符串',
      trigger: 'blur'
    }
  ],
  // 新密码
  newPassword: [
    { required: true, message: '新密码不能为空', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是 6 ~ 15 位的非空字符串',
      trigger: 'blur'
    },
    { validator: checkOldSame, trigger: 'blur' }
  ],
  // 确认新密码
  surePassowrd: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是 6 ~ 15 位的非空字符串',
      trigger: 'blur'
    },
    { validator: checkNewSame, trigger: 'blur' }
  ]
}
</script>

<template>
  <div class="main">
    <HeadContent title="更新密码"></HeadContent>
    <TagComtent></TagComtent>

    <div class="content">
      <TitleContent :img="imgValue" title-name="修改密码" />
      <div class="content-le">
        <el-row>
          <el-col :span="12">
            <el-form :model="pwdForm" :rules="rules" ref="formRef" label-width="100px" size="large">
              <el-form-item label="邮箱" prop="oldPassord">
                <el-input v-model="pwdForm.email" type="text" disabled></el-input>
              </el-form-item>
              <el-form-item label="原密码" prop="oldPassword">
                <el-input v-model="pwdForm.oldPassword" type="password" show-password></el-input>
              </el-form-item>
              <el-form-item label="新密码" prop="newPassword">
                <el-input v-model="pwdForm.newPassword" type="password" show-password></el-input>
              </el-form-item>
              <el-form-item label="确认新密码" prop="surePassowrd">
                <el-input v-model="pwdForm.surePassowrd" type="password" show-password></el-input>
              </el-form-item>
              <el-form-item>
                <el-button @click="onSubmit" type="primary">修改密码</el-button>
                <el-button @click="onReset">重置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100%;
  background-color: #eeeeee;
  h1 {
    font-size: 30px;
  }
  .nav {
    height: 30px;
    margin: 30px 0;
    border: 1px solid #dedddd;
  }
  .content {
    box-shadow: var(--el-box-shadow-light);
    // padding: 20px;
    // background-color: pink;
    height: fit-content;
    border: 1px solid #dedddd;
    .content-le {
      padding: 40px 0 0 30px;
      border: 1px solid #dedddd;
    }
  }
}
</style>
